<?php

namespace XWAM\Controller;

/**
 * Channel 控制器类
 *
 * @since 1.0 <2015-10-22> SoChishun <14507247@qq.com> Added.
 */
class Channel extends AppbaseController {

    public function index() {
        // $where['site_id'] = $this->site_id;
        $where['pid'] = 0;
        $this->assign('type_list', array('DOCUMENT' => array('title' => '文章管理', 'url' => U('Documents/document_list')), 'FILE' => array('title' => '文件管理', 'url' => U('File/file_list'))));
        $mCategory = new \XWAM\Model\DocumentCategoryModel();
        $list = $mCategory->select_tree(array('where' => $where));
        $this->assign('tree', $list);
        $this->display();
    }

    public function edit($id = 0, $pid = 0) {
        $data = array();
        if ($id) {
            $mCategory = new \XWAM\Model\DocumentCategoryModel();
            $data = $mCategory->find($id);
        }
        if (!$data) {
            $data = array('user_name' => $this->userName, 'site_id' => $this->siteId, 'type_name' => 'page');
        }
        if ($pid) {
            $data['pid'] = $pid;
        }
        $atype_names = array(
            array('value' => 'document-list', 'text' => '文章栏目', 'comment' => '用于在此栏目下添加文章 (可以增加下级栏目)'),
            array('value' => 'file-list', 'text' => '文件栏目', 'comment' => '用于在此栏目下添加文件, 如图片或下载 (可以增加下级栏目)'),
            array('value' => 'node', 'text' => '普通节点', 'comment' => '用于上下层级关联, 自身无任何详细内容 (可以增加下级栏目)'), // 2016-6-17 新增
            array('value' => 'page', 'text' => '单独页面', 'comment' => '会生成一个全新的独立页面 (不能增加下级栏目)'),
            array('value' => 'ulink', 'text' => '分类链接', 'comment' => '可以链接到指定分类 (不能增加下级栏目)'),
            array('value' => 'url', 'text' => '链接地址', 'comment' => '增加外链, 如论坛地址 (不能增加下级栏目)'),
        );
        $this->assign('atype_names', $atype_names);
        $this->assign('data', $data);
        $this->display();
    }

    public function editSave() {
        $mCategory = new \XWAM\Model\DocumentCategoryModel();
        $result = $mCategory->save_category();
        $this->dialogJump($result['status'], $result['info']);
    }

    public function get_channel_tree_json($type_name = '', $pid = 0) {
        // $where['site_id'] = $this->site_id;
        if ($type_name) {
            $where['type_name'] = $type_name;
        } else {
            $where['pid'] = $pid;
        }
        $mCategory = new \XWAM\Model\DocumentCategoryModel();
        $data = $mCategory->select_json_tree(array('where' => $where));
        $this->ajaxReturn($data);
    }

    function channel_copy($id = 0) {
        if (!$id) {
            $this->ajaxMsg(false, '参数无效');
        }
        $mCategory = new \XWAM\Model\DocumentCategoryModel();
        $mCategory->copy_category($id);
        $this->ajaxMsg(true);
    }

    function changeStatus($id, $status) {
        $mCategory = new \XWAM\Model\DocumentCategoryModel();
        $this->ajaxReturn($mCategory->changeStatus($id, $status));
    }

    function changeSort($id, $sort) {
        $mCategory = new \XWAM\Model\DocumentCategoryModel();
        $this->ajaxReturn($mCategory->changeSort($id, $sort));
    }

    function channel_delete($id = '') {
        $mCategory = new \XWAM\Model\DocumentCategoryModel();
        $this->ajaxReturn($mCategory->delete_channel($id));
    }

    //********************************link***********************************
    /**
     * link
     * @param type $id
     * @since 1.0 <2015-10-26> SoChishun <14507247@qq.com> Added.
     */
    public function link_edit($id = 0) {
        if (!$id) {
            $this->error('参数有误!');
        }
        $mCategory_link = new \XWAM\Model\DocumentCategoryLinkModel();
        $data = $mCategory_link->find($id);
        if (!$data) {
            $data['category_id'] = $id;
            $data['action'] = 'add';
        }
        $this->assign('data', $data);
        $this->display();
    }

    public function link_edit_save() {
        $mCategory_link = new \XWAM\Model\DocumentCategoryLinkModel();
        $result = $mCategory_link->saveLink();
        if ($result['status']) {
            $this->dialogClose(true, array('success' => '保存成功'));
        } else {
            $this->error(implode('<br />', $result['info']));
        }
    }

    //*******************************content***************************************    

    /**
     * content
     * @param type $id
     * @since 1.0 <2015-10-24> SoChishun <14507247@qq.com> Added.
     */
    public function content_edit($id = 0) {
        if (!$id) {
            $this->error('参数有误!');
        }
        $mCategory_content = new \XWAM\Model\DocumentCategoryContentModel();
        $data = $mCategory_content->find($id);
        if (!$data) {
            $data['category_id'] = $id;
            $data['action'] = 'add';
        }
        $this->assign('data', $data);
        $this->display();
    }

    public function content_edit_save() {
        $mCategory_content = new \XWAM\Model\DocumentCategoryContentModel();
        $result = $mCategory_content->saveContent();
        if ($result['status']) {
            $this->dialogClose(true, array('success' => '保存成功'));
        } else {
            $this->error(implode('<br />', $result['info']));
        }
    }

}
